using System;
using System.Text;
using System.Web.UI;
using Demo.SharePoint.WebParts.OfficeLocator;
using Microsoft.SharePoint;
using System.Runtime.InteropServices;

namespace Demo.SharePoint.ISAPI
{
    [Guid("b3e84cf9-7031-45cb-94de-00d5b79f4fbe")]
    public class LocationRequest : Page
    {
        private SPListItemCollection offices;

        protected void Page_Load(object sender, EventArgs e)
        {
            double r;
            double? radius = (double.TryParse(Request["r"], out r)) ? r : (double?)null;
            offices = OfficeSearch.Search(Request["q"], radius, Request["lurl"]);
            if (offices == null)
                return;

            DisplayOffices();
        }

        private void DisplayOffices()
        {
            int index = 1;
            StringBuilder result = new StringBuilder();
            foreach (SPListItem item in offices)
            {
                result.Append("<div class=\"stm-locationResult\" lid=\"" + item.ID + "\" id=\"locationResult" + index++ + "\" onmouseover=\"STM_locationResultMouseOver(this)\" onmouseout=\"STM_locationResultMouseOut(this)\" onclick=\"STM_locationResultOnClick(this)\">");
                result.Append("<strong>" + item["Title"] + "</strong><br/>");
                result.Append(item["WorkAddress"] + "<br/>");
                //string streetAddr2 = Convert.ToString(item["OtherAddressStreet"]);
                //if (!String.IsNullOrEmpty(streetAddr2))
                //    result.Append(streetAddr2 + "<br/>");
                result.Append(item["WorkCity"] + ", " + item["WorkState"] + " " + item["WorkZip"] + "<br/>");
                result.Append("</div>");
            }

            Response.Write(result);
        }
    }
}